import pandas as pd
from ZIndex.tool import Util as Util
import copy

# 读取csv文件
sign = pd.read_csv(r'.\result\Sign.csv',sep=',')
# sign = sign.sort_values(by='')
# sign 数据去重，因节假日获取为上一交易日
sign = sign.drop_duplicates()
# 数据去重，获取股票列表Series
stock_list = sign.drop_duplicates(['code'])
stock_series = stock_list['code']

result = pd.DataFrame()
result['code'] = stock_list['code']

# 计算各指标准确率
def index_acc(index_name, acc_name, sign, value):
    """
    计算各指标准确率
    :param index_name:
    :param df:
    :return:
    """
    # 逐行处理sign
    # index_name = index_name + 'acc'

    # 获取sign中code与series中code相同的数据，形成新的dataframe
    temp_df = sign[sign['code'].isin([value])]
    # print(temp_df)
    temp_df_row = temp_df.shape[0]
    temp_row_num = 0
    for i in range(0, len(temp_df)):
        temp_row_num = temp_row_num + 1
        # 当前行与前一行数据进行比对，主要针对close价格进行对比，
        if temp_row_num < temp_df_row:  # 最后一行不判断了，默认置为0
            # 买入信号的情况下，通过判断close价格来对信号准确性进行判断
            if temp_df.iloc[i + 1][index_name] > 0:
                # 如果当前行的close价格高于前一日close价格
                if temp_df.iloc[i]['close'] > temp_df.iloc[i + 1]['close']:
                    temp_df.iloc[i + 1, temp_df.shape[1] - 1] = 1
            elif temp_df.iloc[i + 1][index_name] < 0:
                # 如果当前行的close价格低于前一日close价格
                if temp_df.iloc[i]['close'] < temp_df.iloc[i + 1]['close']:
                    temp_df.iloc[i + 1, temp_df.shape[1] - 1] = 1
    # print(temp_df)
    # 获取该组合下个股准确率，先求出准确率不等0的指标有多少个
    sign_num = temp_df[index_name].apply(lambda x: Util.sumSign_fun(x))
    acc_num = temp_df[acc_name].apply(lambda x: Util.sumSign_fun(x))
    # result.loc[value, 'acc'] = acc_num/sign_num
    if sign_num.sum() >0:
        result.loc[index, acc_name] = round(acc_num.sum() / sign_num.sum(), 2)
    else:
        result.loc[index, acc_name] = 0

    return result


# test_Index_List = ['KDJ_S','LWR_S','MACD_S','BRAR_S', 'LB_S','VRSI_S','MA_S','BOLL_S','OBV_S','MFI_S']
test_Index_List = ['KDJ_S','LWR_S','MACD_S','BRAR_S', 'LB_S','VRSI_S','BOLL_S','OBV_S','MFI_S']

# test_Index_List.append('sum')
# 从待选指标中不重复抽样5个指标
# 随机指标个数

for i in range(Util.countMN(len(test_Index_List), 6)):
    select_num = 5
    select_index = Util.listRandomChoice(test_Index_List, select_num)
    # print("#####")
    # print(select_index)
    result_select_index = copy.deepcopy(select_index)
    result_select_index.insert(0, 'time')
    result_select_index.insert(1, 'code')
    result_select_index.insert(2, 'close')

    sign_result_df = sign.loc[:, result_select_index]
    sign_result_df['sum'] = sign_result_df[select_index].apply(lambda x: x.sum(), axis=1)
    tempSign = copy.deepcopy(sign_result_df)
    select_index.append('sum')

    finalResult = pd.DataFrame()
    print(select_index)

    acc_name_list = ['code']
    for column in select_index:
        # print(column)
        index_name = column
        acc_name = index_name + '_acc'
        acc_name_list.append(acc_name)
        for index, value in stock_series.items():
            tempSign[acc_name] = 0
            finalResult = index_acc(index_name, acc_name, tempSign, value)


    # finalResult = finalResult
    # print(finalResult.loc[:, select_index])
    # print(finalResult.loc[:, acc_name_list])

    Util.dataFrameToCsv(finalResult.loc[:, acc_name_list], "/result/Sign_acc_all.csv", True, True)
    # Util.csvAppendLine("/result/Sign_acc_all.csv", '----'.join(result_select_index))
    print(finalResult['sum_acc'])
    print(finalResult['sum_acc'].sum())
    # 求平均准确率
    sum_acc = finalResult['sum_acc'].sum()
    average_acc = round(sum_acc / finalResult.shape[0], 2)

    data = '--'.join(result_select_index)
    data = data + ", 平均准确率为：" + str(average_acc)
    Util.dataToTxt(data, '/result/acc_result.txt', True)




